package com.taomu.app.zongchouhuli.moudle.home.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.http.res.ResMainProductListEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.moudle.home.contract.HomeProductMoreContract;
import com.taomu.app.zongchouhuli.moudle.home.contract.impl.HomeProductMorePresenter;
import com.taomu.app.zongchouhuli.moudle.index.adpter.BaseProductAdp;
import com.taomu.app.zongchouhuli.moudle.index.entity.ProductEntity;
import com.taomu.app.zongchouhuli.utils.ListViewUtils;
import com.taomu.app.zongchouhuli.weight.EmptyRecyclerView;

import java.util.List;

import static com.taomu.app.zongchouhuli.moudle.home.activity.HomeProductDetailActivity.PRODUCTTYPEID;


/**
 * 更多产品
 */
public class HomeProductMoreActivity extends BaseActivity<HomeProductMoreContract.Presenter> implements HomeProductMoreContract.View {

    private ImageView mBackIm;

    private RelativeLayout mTimeRel;

    private TextView mTimeTv;

    private RelativeLayout mIncomeRel;

    private TextView mIncomeTv;

    private EmptyRecyclerView mRecy;

    private BaseProductAdp mAdp;

    private String timeState = "down";

    private String incomeState = "down";

    private SmartRefreshLayout mRefreshLayout;

    private int skip = 0;

    private int take = 10;

    @Override
    protected void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_home_product_more;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.home_productmore_back_im);
        mTimeRel = findViewById(R.id.home_productmore_time_rel);
        mTimeTv = findViewById(R.id.home_productmore_time_tv);
        mIncomeRel = findViewById(R.id.home_productmore_income_rel);
        mIncomeTv = findViewById(R.id.home_productmore_income_tv);
        mRefreshLayout = findViewById(R.id.home_productmore_refresh);
        mAdp = new BaseProductAdp(this);
        mRecy = findViewById(R.id.home_productmore_recy);
        mRecy.setLayoutManager(new LinearLayoutManager(this));
        mRecy.setAdapter(mAdp);
        ListViewUtils.setDefaultEmpty(mRecy);
    }

    @Override
    protected void initData() {
        mRefreshLayout.autoRefresh();
    }

    @Override
    protected void initEvent() {
        mIncomeRel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getIncomeSearch(incomeState);
            }
        });
        mTimeRel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getTimeSearch(timeState);
            }
        });
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAdp.setProductAdpListener(new BaseProductAdp.ProductAdpListener() {
            @Override
            public void onItemClick(ResMainProductListEntity.DataBean entity) {
                HomeProductDetailActivity.start(HomeProductMoreActivity.this, String.valueOf(entity.getId()));
            }
        });
        mRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mRefreshLayout.setEnableLoadMore(true);
                skip = 0;
                mPresenter.getMoreProductList("",String.valueOf(skip),String.valueOf(take));
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.getMoreProductList("",String.valueOf(skip),String.valueOf(take));
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

            }
        });

    }

    @Override
    protected HomeProductMoreContract.Presenter createPresenter() {
        return new HomeProductMorePresenter(this);
    }

    @Override
    public void setMorePorductList(ResMainProductListEntity mData) {
        //mAdp.setData(mData);
        if(mRefreshLayout.isLoading()){
            mAdp.addData(mData.getData());
        }else if(mRefreshLayout.isRefreshing()){
            mAdp.setData(mData.getData());
        }else{
            mAdp.setData(mData.getData());
        }
        skip = mAdp.getItemCount();
        if(Integer.valueOf(mData.getCount())>mAdp.getItemCount()){
            mRefreshLayout.setEnableLoadMore(true);
        }else{
            mRefreshLayout.setEnableLoadMore(false);
        }
    }

    @Override
    public void setTimeResults(List<ProductEntity> mData, String state) {
        timeState = state;
        setDrawRight(mTimeTv,state);
    }

    @Override
    public void setIncomeResults(List<ProductEntity> mData, String state) {
        incomeState = state;
        setDrawRight(mIncomeTv,state);
    }

    private void setDrawRight(TextView titleTv, String state){
        int resid = -1;
        if("down".equals(state)){
            resid = R.drawable.icon_down;
        }else{
            resid = R.drawable.icon_up;
        }
        Drawable drawable = getResources().getDrawable(resid);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
        titleTv.setCompoundDrawables(null, null, drawable, null);//画在右边
    }

    @Override
    public void setPresenter(HomeProductMoreContract.Presenter presenter) {

    }


    @Override
    public void dismissLoadingDialog() {
        super.dismissLoadingDialog();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();

    }
}
